package com.stedu.mall.orderservice.service;

import com.stedu.mall.common.bean.Cart;
import com.stedu.mall.common.bean.Goods;
import com.stedu.mall.common.exception.MallException;
import com.stedu.mall.common.service.CartService;
import com.stedu.mall.common.service.GoodsService;
import com.stedu.mall.orderservice.mapper.CartMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @DubboReference
    private GoodsService goodsService;
    @Override
    public boolean insertOrUpdate(Cart cart) {
        //查询购物车中是否存在该记录
        //1. 如果存在就累加
        //2. 如果不存在就添加
        Cart c = cartMapper.selectByUserIdAndGoodsId(cart.getUserId(), cart.getGoodsId());
        if (c == null) {
            //新增
            cart.setCount(1);
            return cartMapper.insert(cart) == 1;
        }else {
            //修改
            c.setCount(c.getCount() + 1);
            return cartMapper.update(c) == 1;
        }
    }

    @Override
    public boolean delete(Integer id, Integer userId) throws MallException {
        //判断购物车是否存在
        Cart cart = cartMapper.selectById(id);
        if (cart == null) {
            throw new MallException("该购物车不存在，无法删除");
        }
        //判断被删除的购物车是否属于该用户
        if (!cart.getUserId().equals(userId)) {
            throw new MallException("该购物车不属于该用户，无法删除");
        }
        //删除购物车
        return cartMapper.delete(id) == 1;
    }

    @Override
    public boolean deleteByIds(List<Integer> ids, Integer userId) throws MallException {

        for (Integer id : ids) {
            //判断购物车是否都存在
            Cart cart = cartMapper.selectById(id);
            if (cart == null) {
                throw new MallException("该购物车不存在，无法删除");
            }
            //判断被删除的购物车是否属于该用户
            if (!cart.getUserId().equals(userId)) {
                throw new MallException("该购物车不属于该用户，无法删除");
            }
        }

        return cartMapper.deleteByIds(ids) == ids.size();
    }

    @Override
    public boolean update(Cart cart, Integer userId) throws MallException {
        //判断该购物车是否存在
        Cart c = cartMapper.selectById(cart.getId());
        if (c == null) {
            throw new MallException("该购物车记录不存在，无法修改");
        }
        //判断是否属于该用户
        if (!c.getUserId().equals(userId)) {
            throw new MallException("该购物车属于其他用户，无法修改");
        }
        //判断库存是否充足
        //调用goodsService的方法查询对应的商品
        Goods goods = goodsService.selectById(c.getGoodsId());
        if (cart.getCount() != null && cart.getCount() > goods.getCount() ){
            //当购物车商品数量超过库存，就设置商品的数量为库存
            cart.setCount(goods.getCount());
        }
        return cartMapper.update(cart) == 1;
    }

    @Override
    public List<Cart> search(Cart condition) {
        List<Cart> cartList = cartMapper.selectByCondition(condition);
        //调用goodsService的方法查询对应的商品
        for (Cart cart : cartList) {
            cart.setGoods(goodsService.selectById(cart.getGoodsId()));
        }
        return cartList;
    }

    @Override
    public Cart selectById(Integer id) {
        return cartMapper.selectById(id);
    }


    @Override
    public List<Cart> selectByIds(Integer[] cartIds, Integer userId) throws MallException {
        List<Cart> cartList = cartMapper.selectByIds(cartIds, userId);

        //判断查询出来购物车是否属于当前用户
        for (Cart cart : cartList) {
            if (!cart.getUserId().equals(userId)) {
                throw new MallException("当前购物车不属于用户!");
            }
            //查询并设置购物车的商品的信息
            Goods goods = goodsService.selectById(cart.getGoodsId());
            cart.setGoods(goods);
        }


        return cartList;
    }
}
